package com.weetop.akly.activity.meessage;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.weetop.akly.R;
import com.weetop.akly.apiUtils.ApiRetrofit;
import com.weetop.akly.base.BaseFragment;
import com.weetop.akly.base.mvp.BaseModel;
import com.weetop.akly.base.mvp.BaseObserver;
import com.weetop.akly.base.mvp.BasePresenter;
import com.weetop.akly.bean.MsgListBean;
import com.weetop.akly.bean.SalaryBean;
import com.weetop.akly.presenter.MsgListPresent;
import com.weetop.akly.utils.BaseUtils;
import com.weetop.akly.view.MsgListView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MessageFragment extends BaseFragment<MsgListPresent> implements MsgListView {
    @BindView(R.id.rcy_msg)
    RecyclerView rcyMsg;
    Unbinder unbinder;
    @BindView(R.id.srl_data)
    SmartRefreshLayout srData;
    private boolean isRefresh;
    private boolean isViewCreated; // 界面是否已创建完成
    private boolean isVisibleToUser; // 是否对用户可见
    private boolean isDataLoaded; // 数据是否已请求

    @Override
    protected MsgListPresent createPresenter() {
        return new MsgListPresent(this, getActivity());
    }

    private ArrayList<MsgListBean> data = new ArrayList<>();
    private CommonRecyclerAdapter<MsgListBean> adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment_message, null);
        unbinder = ButterKnife.bind(this, view);
//        mPresenter.getHomeMsgList();
        srData.finishLoadMoreWithNoMoreData();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getHomeMsgList();
    }

    @Override
    public void getMsgListSuccess(BaseModel<ArrayList<MsgListBean>> o) {
        if (isRefresh) {
            isRefresh = false;
            srData.finishRefresh(true);
        }
        if (o.getData() != null) {
            data.clear();
            data.add(o.getData().get(0));
            data.add(o.getData().get(1));
            initMsgList();
        }
    }

    protected void initMsgList() {

        rcyMsg.setItemAnimator(null);
        rcyMsg.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new CommonRecyclerAdapter<MsgListBean>(mActivity, R.layout.item_msg, data) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, MsgListBean item, int position) {
                ImageView iv_function = helper.getView(R.id.iv_function);
                ImageView iv_devide = helper.getView(R.id.iv_devide);
                TextView tv_isRead = helper.getView(R.id.tv_isread);

                if ("打卡".equals(item.getModel_name())) {
                    Glide.with(mActivity).load(R.mipmap.icon_daka).into(iv_function);
                } else {
                    Glide.with(mActivity).load(R.mipmap.icon_shenpi).into(iv_function);
                }
                helper.setText(R.id.tv_title, item.getModel_name());
                helper.setText(R.id.tv_desc, item.getNotes());
                String timeState = BaseUtils.getTimeStateNew(item.getCreate_time() + "");
                helper.setText(R.id.tv_time, timeState);
                int is_read = item.getIs_read_two();
                if (is_read == 0) {
//                    tv_isRead.setText(is_read + "");
                    tv_isRead.setVisibility(View.VISIBLE);
                } else {
                    tv_isRead.setVisibility(View.INVISIBLE);
                }
                if (position == data.size() - 1) {
                    iv_devide.setVisibility(View.GONE);
                }
            }
        };
        rcyMsg.setAdapter(adapter);
        srData.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                srData.finishLoadMore(2000);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                isRefresh = true;
                mPresenter.getHomeMsgList();
            }
        });
        adapter.setOnItemClickListener((viewHolder, view1, position) -> {
            if (BaseUtils.fastClick()) {
                if (position == 0) {
                    BaseUtils.toActivity(mActivity, SignDayRecordActivity.class);
                } else {
                    BaseUtils.toActivity(mActivity, ShenpiListActivity.class);
                }
            }
        });
    }




    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
